package com.das.mechanic_base.adapter.alone;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.alone.AloneNewBean;
import com.das.mechanic_base.def.alone.AloneSubmitStatus;
import com.das.mechanic_base.utils.X3StringUtils;
import com.hjq.toast.ToastUtils;

import java.util.List;

public class X3NumAdapter extends RecyclerView.Adapter<X3NumAdapter.NumHolder> {

    private List<String> mList;
    private List<String> answer;

    private List<AloneNewBean.ItemQuestionListBean.QuestionDetectionTempletQuestionListBean> questionList;

    private Context mContext;

    private int titleIndex;

    public X3NumAdapter(Context mContext, List<String> mList, List<AloneNewBean.ItemQuestionListBean.QuestionDetectionTempletQuestionListBean> questionList,List<String> answer, int titleIndex) {
        this.mList = mList;
        this.mContext = mContext;
        this.answer = answer;
        this.titleIndex = titleIndex;
        this.questionList = questionList;
    }

    @NonNull
    @Override
    public NumHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new NumHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_num_alone_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull NumHolder holder, int position) {
        if (X3StringUtils.isListEmpty(mList)) {
            return;
        }

        if (position == 0){
            holder.v_divider.setVisibility(View.GONE);
        }else{
            holder.v_divider.setVisibility(View.VISIBLE);
        }

        holder.tv_num.setText((position + 1) +".");

        holder.v_status.setVisibility(View.VISIBLE);
        holder.tv_name.setTextColor(ContextCompat.getColor(mContext,R.color.c333));
        holder.tv_num.setTextColor(ContextCompat.getColor(mContext,R.color.c333));
        String status = mList.get(position);
        String answerContent = "";
        String itemName = "";
        try{
            itemName = questionList.get(position).description;
            answerContent = answer.get(position);
        }catch (Exception e){
            e.printStackTrace();
        }

        holder.tv_name.setText(itemName +":  " + answerContent);

        if (X3StringUtils.isEmpty(status)) {
            status = "";
        }
        switch (status) {
            case AloneSubmitStatus.PASS:
                holder.v_status.setImageResource(R.mipmap.x3_alone_item_pass);
//                holder.v_status.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_alone_show_num_normal_vbg));
//                holder.tv_num.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_alone_show_num_normal));
                break;
            case AloneSubmitStatus.MINOR:
                holder.v_status.setImageResource(R.mipmap.x3_alone_item_warning);
//                holder.v_status.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_alone_show_minor_normal_vbg));
//                holder.tv_num.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_alone_show_minor_normal));
                break;
            case AloneSubmitStatus.MAJOR:
            case AloneSubmitStatus.DANGEROUS:
                holder.v_status.setImageResource(R.mipmap.x3_alone_item_danger);
//                holder.v_status.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_alone_show_num_dangerous_vbg));
//                holder.tv_num.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_alone_show_num_dangerous));
                break;
            default:
                holder.tv_name.setTextColor(Color.parseColor("#B1B3BD"));
                holder.tv_num.setTextColor(Color.parseColor("#B1B3BD"));
                holder.v_status.setImageResource(R.mipmap.x3_alone_item_no);
                holder.tv_name.setText(itemName +":  N/A");
//                holder.tv_num.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_alone_show_num_no_select));
                break;
        }

        holder.itemView.setOnClickListener(v -> {
            if (iOnClickJump != null) {
                /**
                 * 这里是因为 外部父view包含
                 * 最顶部一个 titleview
                 * 检测示例
                 * 拍照
                 * 录音模块
                 * 每一个检测项的分割线
                 */
                iOnClickJump.iOnClickJumpToIndex(titleIndex,position);
            }
        });

    }

    IOnClickJump iOnClickJump;

    public void setiOnClickJump(IOnClickJump iOnClickJump) {
        this.iOnClickJump = iOnClickJump;
    }

    public interface IOnClickJump {
        void iOnClickJumpToIndex(int titleIndex,int index);
    }

    @Override
    public int getItemCount() {
        if (X3StringUtils.isListEmpty(mList)) {
            return 0;
        }
        return mList.size();
    }

    class NumHolder extends RecyclerView.ViewHolder {
        TextView tv_num;
        TextView tv_name;
        ImageView v_status;
        View v_divider;
        public NumHolder(@NonNull View itemView) {
            super(itemView);
            tv_num = itemView.findViewById(R.id.tv_num);
            v_status = itemView.findViewById(R.id.v_status);
            tv_name = itemView.findViewById(R.id.tv_name);
            v_divider = itemView.findViewById(R.id.v_divider);
        }
    }
}
